package interceprtor;
import Util.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;
public class InterceptorChain {
    private static List<Interceptor> interceptors;
    private static Iterator<Interceptor> iterator;
    private HandlerDefinition definition;
    public HandlerDefinition getDefinition() {
        return definition;
    }
    public void setDefinition(HandlerDefinition definition) {
        this.definition = definition;
    }
    public InterceptorChain(){
        interceptors = HandlerDefinitionparse.getInterceptor();
        iterator=interceptors.iterator();
    }
    public Object execute(){
        Object result=null;
        if (iterator.hasNext()){
               result=iterator.next().interceptor(this);
        }else{
            try {
                HandlerInvoke invoke=new HandlerInvoke();
                result = invoke.Invoke(getDefinition());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
